#include <iostream>
#include <stdio.h>
using namespace std;

// Parameters:
// numbers:     an array of integers
//length:      the length of array numbers
//duplication: (Output) the duplicated number in the array number
//Return value:       true if the input is valid, and there are some duplications in the array number
//otherwise false
bool duplicate(int numbers[], int length, int* duplication) 
{
    if(numbers==NULL)
    {
        return false;
    }
    if(length<=1)
    {
        return false;
    }
    if(duplication==NULL)
    {
        return false;
    }
            
    int *p=new int[length];
    //const int N=length; 
    //int p[N]={0};

    int i=0;
    int flag=0;
    for(i=0;i<length;++i)
    {
        if(numbers[i]<0||numbers[i]>=length)
        {
            flag=1;
        }
        p[i]=0;
    }
    if(flag)
    {
        return false;
    }
    for(i=0;i<length;++i)
    {
        if(++p[numbers[i]]>=2)
        {
            *duplication=numbers[i];
            return true;
        }
    }
    delete [] p;
    return false;
}

void test()
{
    const int N=7;
    int numbers[N]={1,2,3,4,4,5,6};
    int duplication=0;
    bool ret=duplicate(numbers,N,&duplication);
    if(ret)
    {
        cout<<"duplication:"<<duplication<<endl;
    }
    else
    {
        cout<<"error"<<endl;
    }
}

int main()
{
    test();
    return 0;
}
